﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;

public partial class Accountant_SelectSales : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    private void Bind()
    {
        string company = Request.QueryString["CompanyID"];
        string orderid = Request.QueryString["orderid"];
        GridView1.DataSource = new AuditorsBLL().GetCompanySales(company, orderid);
        GridView1.DataBind();


        var AuditorsEntity = new CompanyBLL().GetCompanyAuditorsEntityByOrderId(orderid);
        if (AuditorsEntity != null)
        {

            List<ContrastAmount> banks = new AuditorsBLL().GetBankSalesAmount(company, orderid, ((int)ExplainEnum.Sales).ToString());
            List<ContrastAmount> sales = new AuditorsBLL().GetSalesAmount(company, orderid);

            StringBuilder sb = new StringBuilder();
            sb.Append("<table>");
            sb.Append("<tr>");
            sb.AppendFormat("<th>{0}</th>", "日期");
            sb.AppendFormat("<th>{0}</th>", "银行流水");
            sb.AppendFormat("<th>{0}</th>", "发票金额");
            sb.AppendFormat("<th>{0}</th>", "差额");
            sb.Append("</tr>");

            for (DateTime dt = AuditorsEntity.StartTime; dt <= AuditorsEntity.EndTime; dt = dt.AddMonths(1))
            {
                double d = 0;
                sb.Append("<tr>");
                sb.AppendFormat("<th>{0}</th>", dt.ToString("yyyy年MM月"));
                if (banks.Count(fun => fun.Date.Year == dt.Year && fun.Date.Month == dt.Month) > 0)
                {
                    d = banks.First(fun => fun.Date.Year == dt.Year && fun.Date.Month == dt.Month).Amont;
                    sb.AppendFormat("<th>{0}</th>", d);
                }
                else
                {
                    sb.AppendFormat("<th>{0}</th>", 0);
                }

                if (sales.Count(fun => fun.Date.Year == dt.Year && fun.Date.Month == dt.Month) > 0)
                {
                    d = d - sales.First(fun => fun.Date.Year == dt.Year && fun.Date.Month == dt.Month).Amont;
                    sb.AppendFormat("<th>{0}</th>", sales.First(fun => fun.Date.Year == dt.Year && fun.Date.Month == dt.Month).Amont);
                }
                else
                {
                    sb.AppendFormat("<th>{0}</th>", 0);
                }

                sb.AppendFormat("<th>{0}</th>", d);
                sb.Append("</tr>");
            }

            sb.Append("</table>");

            Label2.Text = sb.ToString();
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int Currency = Convert.ToInt32(e.Row.Cells[6].Text);
            CurrencyEnum CStatus = (CurrencyEnum)Currency;
            e.Row.Cells[6].Text = RemarkAttribute.GetEnumRemark(CStatus);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string company = Request.QueryString["CompanyID"];
        string orderid = Request.QueryString["orderid"];

        int i = new AuditorsBLL().CalculationCompanySales(company, orderid);

        if (i == 0)
        {
            Response.Redirect("selectPurchase.aspx?CompanyID=" + company + "&orderid=" + orderid);
        }
        else
        {
            AlterAndRedirect(string.Format("还差{0}金额", i), "selectPurchase.aspx?CompanyID=" + company + "&orderid=" + orderid);
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        new AuditorsBLL().DeleteCompanySales(e.Keys[0].ToString());

        Bind();
    }
}